<?php
/**
* 功能: 生成饼状图
* 创建日期:Mon Dec 12 13:23:44 CST 2011
* 最后更新:
* 作者: slash
*/

include('ThePage.class.php');

$db = Factory::getDB('zy_jobs_week');
$act = isset($_GET['act']) ? $_GET['act'] : '';


$title = iconv("UTF-8","GB2312","工作统计");
$year_where = "week_time like '".date('Y')."%' and states='finished'";


$week_where = "week_time='".date('Y')."-".date('W')."'";



$p_week_where = $year_where."  group by project";
$arr = $db->getAll($p_week_where,'',-1,-1,'project');

if(isset($arr[0])){
	
	foreach ($arr as $k=>$v){
		//求出年总数
		$w_year_where = $year_where." and project={$v['project']}";
		//$all_num = $db->getSumNumber('zy_jobs_week',$w_week_where);
		//$f_year_where = $w_year_where." and states='finished'";
		$f_num = $db->getSumNumber('zy_jobs_week',$w_year_where);
		//print_r($db->getDebug());
		//$data1y[] = ($all_num-$f_num);
		$datay2[] = $f_num?$f_num:0;
		//求月
		$w_week_where = $week_where." and project={$v['project']}";
		//$all_num = $db->getSumNumber('zy_jobs_week',$w_week_where);
		$f_week_where = $w_week_where." and states='finished'";
		$w_num = $db->getSumNumber('zy_jobs_week',$f_week_where);
		//print_r($db->getDebug());
		//$data1y[] = ($all_num-$f_num);
		$datay[] = $w_num?$w_num:0;
		
		$alts[] = get_p_name($v['project']);
		$targ[] = '/index.php?c=zy_week_report&project='.$v['project'];
	}
}
//print_r($alts);
/*print_r($data1);
print_r($data2);
print_r($alst1);
print_r($targ);exit;*/
if(empty($datay2)){
	echo "<img src='/pic/no_pic.gif' >";
	exit;
}
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');
$count = count($datay2);
/*$datay=array(20,30,50,80);
$datay2=array(430,645,223,690);*/
$datazero=array(0,0,0,0);

// Create the graph. 
$graph = new Graph(75*$count,200,'auto');
$graph->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->title->Set($title);

// Setup Y and Y2 scales with some "grace"	
$graph->SetScale("textlin");
$graph->SetY2Scale("lin");
$graph->yaxis->scale->SetGrace(30);
$graph->y2axis->scale->SetGrace(30);

//$graph->ygrid->Show(true,true);
$graph->ygrid->SetColor('gray','lightgray@0.5');

// Setup graph colors
$graph->SetMarginColor('white');
$graph->y2axis->SetColor('darkred');


// Create the "dummy" 0 bplot
$bplotzero = new BarPlot($datazero);

// Create the "Y" axis group
$ybplot1 = new BarPlot($datay);
$ybplot1->SetCSIMTargets($targ,$alts);
$ybplot1->value->Show();
$ybplot = new GroupBarPlot(array($ybplot1,$bplotzero));

// Create the "Y2" axis group
$ybplot2 = new BarPlot($datay2);
$ybplot2->SetCSIMTargets($targ,$alts);
$ybplot2->value->Show();
$ybplot2->value->SetColor('darkred');
$ybplot2->SetFillColor('darkred');
$y2bplot = new GroupBarPlot(array($bplotzero,$ybplot2));

// Add the grouped bar plots to the graph
$graph->Add($ybplot);
$graph->AddY2($y2bplot);

// .. and finally stroke the image back to browser
$graph->StrokeCSIM();


/*
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');

$count = count($data1y);
// Create the graph. These two calls are always required
$graph = new Graph(120*$count,200,'auto');
$graph->SetScale("textlin");
$graph->img->SetMargin(40,30,20,40);
$graph->SetShadow();

// Create the bar plots
$b1plot = new BarPlot($data1y);
$b1plot->SetFillColor("orange");
//$targ=array("bar_clsmex2.php#1","bar_clsmex2.php#2","bar_clsmex2.php#3",
//"bar_clsmex2.php#4","bar_clsmex2.php#5","bar_clsmex2.php#6");
//$alts=array("val=%d","val=%d","val=%d","val=%d","val=%d","val=%d");
$b1plot->SetCSIMTargets($targ,$alts);

$b2plot = new BarPlot($data2y);
$b2plot->SetFillColor("blue");
//$targ=array("bar_clsmex2.php#7","bar_clsmex2.php#8","bar_clsmex2.php#9",
//"bar_clsmex2.php#10","bar_clsmex2.php#11","bar_clsmex2.php#12");
//$alts=array("内容1","内容1=%d","内容1=%d","内容1=%d","内容1=%d","内容1=%d");
$b2plot->SetCSIMTargets($targ,$alts);

// Create the grouped bar plot
$abplot = new AccBarPlot(array($b1plot,$b2plot));

$abplot->SetShadow();
$abplot->value->Show();

// ...and add it to the graPH
$graph->Add($abplot);

$graph->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->title->Set($title);
$graph->xaxis->title->Set("X-title");
$graph->yaxis->title->Set("Y-title");



//$graph->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);


// Send back the HTML page which will call this script again
// to retrieve the image.
$graph->StrokeCSIM();

*/



function get_p_name($id)
{
	$db = Factory::getDB('zy_project');
	$arr = $db->getOne($id);
	return $arr['project_name'];
}